/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CompanyListPanel.java
 *
 * Created on 08 Feb 2011, 1:13:02 PM
 */
package za.co.pas.business.view.panel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.util.List;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import za.co.pas.business.controller.CompanyDetailController;
import za.co.pas.business.data.Company;
import za.co.pas.business.view.model.list.CompanyListModel;
import za.co.pas.business.message.StatusMessage;
import za.co.pas.business.abstracts.AbstractView;

/**
 *
 * @author alabuschagne
 */
public class CompanyListPanel extends JPanel implements AbstractView
{
    private boolean dirty;
    private CompanyListModel companyListModel = new CompanyListModel();
    private CompanyDetailController companyDetailController;

    public CompanyListPanel()
    {
    }

    /** Creates new form CompanyListPanel */
    public CompanyListPanel(CompanyDetailController companyDetailController)
    {
        super();
        this.companyDetailController = companyDetailController;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        companiesList = new javax.swing.JList();

        setLayout(new java.awt.BorderLayout());

        companiesList.setModel(companyListModel);
        companiesList.setToolTipText("Right click for menu");
        companiesList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                companiesListMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(companiesList);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void companiesListMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_companiesListMouseReleased
    {//GEN-HEADEREND:event_companiesListMouseReleased
        if (evt.isPopupTrigger())
        {
            JPopupMenu Pmenu = new JPopupMenu();

            JMenuItem menuItem = new JMenuItem("New");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    editCompany(null);
                }
            });
            int k = companiesList.getSelectedIndex();
            if (k != -1)
            {
                menuItem = new JMenuItem("Edit");
                Pmenu.add(menuItem);
                menuItem.addActionListener(new ActionListener()
                {

                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        int k = companiesList.getSelectedIndex();
                        if (k != -1)
                        {
                            Company c = (Company) companyListModel.getElementAt(k);
                            editCompany(c);
                        }
                    }
                });
            }
            Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_companiesListMouseReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList companiesList;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt)
    {
        if (evt.getPropertyName().compareTo(StatusMessage.COMPANY_LIST_CHANGED) == 0)
        {
            if (evt.getNewValue() instanceof List)
            {
                companyListModel.setCompanies((List<Company>) evt.getNewValue());
                companiesList.updateUI();
            }
        }
    }

    private void editCompany(Company c)
    {
        if (c == null)
        {
            companyDetailController.requestMakeNewCompany();
        }
        else
        {
            companyDetailController.requestEditCompany(c);
        }
    }

    @Override
    public boolean isDirty()
    {
        return dirty;
    }

    /**
     * @param dirty the dirty to set
     */
    public void setDirty(boolean dirty)
    {
        this.dirty = dirty;
    }

    @Override
    public String getExtraTitle()
    {
        return null;
    }
}
